define([ 'jquery',
  'backbone',
  'require.dust!./programs-template',
  'partner/shra/js/hra/jquery.tools.overlay',
  './pubsub',
   './widgetViews',
  'jquery-ui'
], function( $, Backbone, template, _overlay, pubsub, WidgetViews ) {
  'use strict';

  var ProgramView = Backbone.View.extend({
    _$tabs : null,
    _widgetView : null,
    _widgetModel: null,

    initialize : function() {
       var self = this,
           item = self.model.toJSON(),
           widget = null,
           widgetType = null,
           widgetClass = null,
           index = self.model.get('index');
      console.dir(this.model);
      self.$el = $( '#hra-programs-tabs-' + self.model.get( 'index' ));
//      template( {'programs': programsdata}, function( err, out ){

//        $.each( programsdata, function( index, item ){
          //Todo: remove later when real data is ready
          if( index === 0 &&  WidgetViews[ item[ 'widgets' ][ item.currentIndex ][ 'type' ] ]  ) {
            widget = item[ 'widgets' ][ item.currentIndex ];
            widgetType = widget[ 'type' ];
            widgetClass = WidgetViews[ widgetType ];
          }
//        });

//      });
    },

    render : function() {


    },
    _loadWidget: function( widgetClass, widget, index ) {
      var self = this;
//      var widgetView = null,
//        widgetModel = null;
      if(self._widgetView)self._widgetView.remove();

      self._widgetModel = new Backbone.Model({
          content : widget.content,
          index : index
      });

      self._widgetView = new ( widgetClass )( { model : self._widgetModel } ) ;


    },
    _reload : function( index ) {
      var program = this.model.at( index ),
        widgets = program.get( 'widgets' ),
        widget = widgets[ program.get( 'currentIndex' ) ],
        widgetType = widget[ 'type' ],
        widgetClass = WidgetViews[ widgetType ];

      this._loadWidget( widgetClass, widget, index );



    },

    events : {

    },


    remove: function() {
      this.$el.hide();
    }

  });



  var ProgramViews = Backbone.View.extend({

    el : '#hra-programs',
    _$tabs : null,
    _programViews: [],

    initialize : function() {
       var self = this,
           programsdata = [],
           widget = null,
           widgetType = null,
           widgetClass = null,
           programModel = null,
           programView = null;
//      this.listenTo( this.model, 'change', this.render );
//      this.model.fetch().done(function() {
//        console.dir(this.model);
//      });
      this.model.fetch();
      programsdata = self.model.toJSON();
      console.dir(this.model);
      template( {'programs': programsdata}, function( err, out ){
        self.$el.append( out );
        self._$tabs = $( '#hra-programs-tabs' ).tabs({
          collapsible : true
        });
        $.each( programsdata, function( index, item ){
          //Todo: remove later when real data is ready
          if( index === 0 &&  WidgetViews[ item[ 'widgets' ][ item.currentIndex ][ 'type' ] ]  ) {
            widget = item[ 'widgets' ][ item.currentIndex ];
            widgetType = widget[ 'type' ];
            widgetClass = WidgetViews[ widgetType ];
            programModel = item;
            programView = new ProgramView( { model : programModel } ) ;
            self._programViews.add(programView);

            self._loadWidget( widgetClass, widget, index );

          }
        });

      });
    },

    render : function() {

    },

    _loadWidget: function( widgetClass, widget, index ) {
      var self = this;
//      var widgetView = null,
//        widgetModel = null;
      if(self._widgetView)self._widgetView.remove();

      self._widgetModel = new Backbone.Model({
          content : widget.content,
          index : index
      });

      self._widgetView = new ( widgetClass )( { model : self._widgetModel } ) ;


    },
    _reload : function( index ) {
      var program = this.model.at( index ),
        widgets = program.get( 'widgets' ),
        widget = widgets[ program.get( 'currentIndex' ) ],
        widgetType = widget[ 'type' ],
        widgetClass = WidgetViews[ widgetType ];

      this._loadWidget( widgetClass, widget, index );



    },

    events : {     
     
    },


    remove: function() {
      this.$el.hide();
    }

  });

  return ProgramViews;

});
